/** Copyright (c) 2008-2009, Markos Evlogimenos
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * The name of Markos Evlogimenos may be used to endorse or
 *		 promote products derived from this software without specific
 *		 prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package org.kamimark.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

import org.kamimark.properties.sorters.InsertionSortedProperties;

public class PropertiesBaseReader
{

	private final Hashtable<String, Properties> propertyFiles = new Hashtable<String, Properties>();

	public String getProperty(final String fileName, final String key) {
		Properties properties;
		if (propertyFiles.containsKey(fileName))
			properties = propertyFiles.get(fileName);
		else
			properties = loadPropertyFile(fileName);

		if (properties.containsKey(key))
			return properties.getProperty(key);

		// Following code is only run in case of missing files.

		final String value = key.toLowerCase();
		properties.setProperty(key, value);

		final File propertyFile = Files.getResourceProperty(fileName);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(propertyFile);
			properties.store(fos, "This file was autogenerated. Please set correct properties.If this file has messed up order. Delete it and rerun the software.");
			fos.close();
		} catch (final FileNotFoundException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
		return value;
	}

	private Properties loadPropertyFile(final String fileName) {
		final String fullFileName = fileName;

		try {

			final boolean exists = Files.resourcePropertyExist(fullFileName);
			final File propertyFile = Files.getResourceProperty(fullFileName);
			final FileInputStream fis = new FileInputStream(propertyFile);

			Properties properties = null;
			if (exists)
				properties = new Properties();
			else
				properties = new InsertionSortedProperties();

			properties.load(fis);

			propertyFiles.put(fileName, properties);

			fis.close();
			return properties;

		} catch (final IOException e) {
			e.printStackTrace();
		}

		return null;

	}

}
